package br.com.dwii.tpg2.servlet;

import br.com.dwii.tpg2.entidade.Animal;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Diego
 */
public class BuscaSrv extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        StringBuilder sqlWhere = new StringBuilder();
        // Tipo de animal
        if (request.getParameter("tipo_animal_1") != null && request.getParameter("tipo_animal_2") == null) {
            sqlWhere.append(" and c.flag_tipo_animal = 'C' ");
        } else if (request.getParameter("tipo_animal_1") == null && request.getParameter("tipo_animal_2") != null) {
            sqlWhere.append(" and c.flag_tipo_animal = 'G' ");
        }
        // Sexo
        if (request.getParameter("sexo_1") != null && request.getParameter("sexo_2") == null) {
            sqlWhere.append(" and c.flag_sexo = 'M' ");
        } else if (request.getParameter("sexo_1") == null && request.getParameter("sexo_2") != null) {
            sqlWhere.append(" and c.flag_sexo = 'F' ");
        }
        // Idade
        if (request.getParameter("idade_1") != null && request.getParameter("idade_2") == null) {
            sqlWhere.append(" and c.flag_idade = 'F' ");
        } else if (request.getParameter("idade_1") == null && request.getParameter("idade_2") != null) {
            sqlWhere.append(" and c.flag_idade = 'A' ");
        }
        // Porte
        if (request.getParameter("porte_1") != null || request.getParameter("porte_2") != null || request.getParameter("porte_3") != null) {
            sqlWhere.append(" and (");
            boolean addOr = false;
            if (request.getParameter("porte_1") != null) {
                sqlWhere.append("c.flag_porte = 'P'");
                addOr = true;
            }
            if (request.getParameter("porte_2") != null) {
                if (addOr) {
                    sqlWhere.append(" or ");
                }
                sqlWhere.append("c.flag_porte = 'M'");
                addOr = true;
            }
            if (request.getParameter("porte_3") != null) {
                if (addOr) {
                    sqlWhere.append(" or ");
                }
                sqlWhere.append("c.flag_porte = 'G'");
            }
            sqlWhere.append(") ");
        }
        // Pelagem
        if (request.getParameter("pelagem_1") != null && request.getParameter("pelagem_2") == null) {
            sqlWhere.append(" and c.flag_pelagem = 'C' ");
        } else if (request.getParameter("pelagem_1") == null && request.getParameter("pelagem_2") != null) {
            sqlWhere.append(" and c.flag_pelagem = 'L' ");
        }
        // Raça
        if (request.getParameter("raca_1") != null && request.getParameter("raca_2") == null) {
            sqlWhere.append(" and c.flag_raca = 'S' ");
        } else if (request.getParameter("raca_1") == null && request.getParameter("raca_2") != null) {
            sqlWhere.append(" and c.flag_raca = 'C' ");
        }
        // Saúde
        //sqlWhere.append(" and c.flag_esterilizado = ").append(request.getParameter("saude_1") == null ? "'N' " : "'S' ");
        //sqlWhere.append(" and c.flag_vacinado = ").append(request.getParameter("saude_2") == null ? "'N' " : "'S' ");
        //sqlWhere.append(" and c.flag_vermifugado = ").append(request.getParameter("saude_3") == null ? "'N' " : "'S' ");
        
        try {
            request.getSession().setAttribute("lstAnimaisBusca", Animal.getListBusca(sqlWhere.toString()));
        } catch (SQLException ex) {
            Logger.getLogger(BuscaSrv.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        response.sendRedirect("resultado.jsp");
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
